define([
    'require'
], function(require, factory) {
    'use strict';
    /**
     * 工具栏
     * @class
     * @param {*} query 
     */
    function ToolBar(query){
        this._context =document.querySelector('#'+query+' ul');
        this._size = 0;
        this._tools = {};           // toolBar对应的元素
        this._events = {};          // toolBar对应的事件
    }
    
    Object.defineProperties(ToolBar, {
        size : {
            get : function(){
                return this._size;
            }
        }
    })
    /**
     * 添加工具栏
     * @param id 工具栏ID
     * @param name 工具栏内容
     * @param callback 点击事件 
     */
    ToolBar.prototype.addToolBar = function(id, name, callback){
        if(typeof id === 'undefined' || typeof name === 'undefined' || typeof callback !== 'function'){
            throw new Error('options.text and callback is required!')
        }
        
        if(typeof this._tools[id] === 'undefined'){
            // 创建元素
            var ele = document.createElement("li");
            ele.innerText = name;
            this._context.appendChild(ele);
            this._size ++;
            this._tools[id] = ele;
            // 添加事件
            this._events[id] = new Array();
            var that = this;
            ele.addEventListener('click', function(){
                that._releaseEvent(id);
            });
        }
        // 绑定事件
        this._events[id].push(callback);
        return function(){
            this.removeToolBar(id, callback);
        }
    }
    /**
     * 移除工具栏
     * @param id 工具栏ID
     * @param callback 点击事件 
     */
    ToolBar.prototype.removeToolBar = function(id){
        var parent = this._context;
        var child = this._tools[id];
        // 移除事件和元素
        parent.removeChild(child);
        // 删除属性
        del(this._tools[id]);
        del(this._events[id]);
        this.size --;
    }
    /**
     * 响应事件
     * @param id 工具栏ID
     */
    ToolBar.prototype._releaseEvent = function(id, ...args){
        var events = this._events[id];
        for(var i = 0;i<events.length; i++){
            events[i].call(null, ...args);
        }
    }
    /**
     * 添加输入框
     * @param {*} id 
     * @param {*} name 
     * @param {*} callback 
     */
    ToolBar.prototype.addInput = function(id, name, callback){
        if(typeof id === 'undefined' || typeof name === 'undefined' || typeof callback !== 'function'){
            throw new Error('options.text and callback is required!')
        }
        
        if(typeof this._tools[id] === 'undefined'){
            // 创建元素
            var ele = document.createElement("li");
            ele.innerHTML = `
                <input placeholder='${name}' id='input-${id}' ></input>
            `;
            this._context.appendChild(ele);
            this._size ++;
            this._tools[id] = ele;
            // 添加事件
            this._events[id] = new Array();
            var that = this;
            ele.addEventListener('keydown', function(e){
                if(e.keyCode === 13){
                    that._releaseEvent(id, e.target.value);
                }
            });
        }
        // 绑定事件
        this._events[id].push(callback);
        return function(){
            this.removeToolBar(id, callback);
        }
    }
    return ToolBar;
});